package com.iretailer.report.analyze.netty;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpContentCompressor;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpRequestDecoder;
import io.netty.handler.codec.http.HttpResponseEncoder;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Auther: xjw
 * @Date: 2018/12/4 17:57
 * @Description:
 */
public class NettyServerHandlerInit extends ChannelInitializer<SocketChannel> {

    private AtomicInteger connectNum;

    public NettyServerHandlerInit(AtomicInteger connectNum) {
        this.connectNum = connectNum;
    }

    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        // Create a default pipeline implementation.
        ChannelPipeline pipeline = ch.pipeline();

        /**
         * http-request解码器
         * http服务器端对request解码
         */
        pipeline.addLast("decoder", new HttpRequestDecoder());
        /**
         * http-response解码器
         * http服务器端对response编码
         */
        pipeline.addLast("encoder", new HttpResponseEncoder());
        //聚合http请求
        pipeline.addLast("aggre", new HttpObjectAggregator(10 * 1024 * 1024));
        //启用http压缩
        pipeline.addLast("compressor", new HttpContentCompressor());

        pipeline.addLast("handler", new NettyServerHandler(connectNum));

    }


}
